package ts.group.webapp.market.controller.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ts.group.webapp.market.common.core.util.ResponseData;
import ts.group.webapp.market.common.core.web.controller.BaseController;
import ts.group.webapp.market.model.TProductInfo;
import ts.group.webapp.market.service.IProdService;

import com.starit.common.dao.support.Pagination;
import com.starit.common.dao.support.PaginationRequest;

@Controller
@RequestMapping("/prod")
public class ProdController extends BaseController{
	
	@Autowired
	private IProdService IProdService;
	
	/**
	 * 新增产品，只接受POST请求
	 */
	@RequestMapping(value = "/insertTprod", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData insertTprod(TProductInfo prod) throws Exception {
		List<TProductInfo> prodList = IProdService.getProdByCode(prod.getProductCode());
		if(!prodList.isEmpty()){
			return new ResponseData(true, "产品编号重复");
		}
		IProdService.createProd(prod);
		return ResponseData.SUCCESS_NO_DATA;
	}
	
	
	/**
	 * 更新产品, 只接受POST请求
	 */
	@RequestMapping(value = "/updateTprod", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData updateTprod(TProductInfo prod) {
		TProductInfo old = IProdService.getProdById(prod.getId());
		old.setProductName(prod.getProductName());
		old.setProductPrice(prod.getProductPrice());
		old.setProductBv(prod.getProductBv());
		old.setProductPv(prod.getProductPv());
		old.setStatus(prod.getStatus());
		old.setRemark(prod.getRemark());
		IProdService.updateProd(prod);
		return ResponseData.SUCCESS_NO_DATA;
	}
	
	/**
	 * 批量删除产品, 只接受POST请求
	 */
	@RequestMapping(value = "/removeTprod", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData removeTprod(Long[] ids) {
		for(Long id : ids){
			IProdService.removeProd(id);
		}
		return ResponseData.SUCCESS_NO_DATA;
	}

	
	/**
	 * 分页查询产品
	 */
	@RequestMapping("/pageQueryTprod")
	@ResponseBody
	public Pagination<TProductInfo> pageQueryTprod(
			@RequestParam("start") int startIndex,
			@RequestParam("limit") int pageSize, TProductInfo prod,
			@RequestParam(required = false) String sort,
			@RequestParam(required = false) String direction){
		
		PaginationRequest<TProductInfo> pageRequest = new PaginationRequest<TProductInfo>(startIndex, pageSize);
		if(StringUtils.hasText(sort) && StringUtils.hasText(direction)){
			pageRequest.addOrder(sort, direction);
		}
		
		if (StringUtils.hasText(prod.getProductCode())) {
			pageRequest.addLikeCondition("productCode", prod.getProductCode());
		}
		if (StringUtils.hasText(prod.getProductName())) {
			pageRequest.addLikeCondition("productName", prod.getProductName());
		} 
		
		Pagination<TProductInfo> pageList = IProdService.findAllForPage(pageRequest);
		for(TProductInfo p : pageList.getResult()){
			if(p.getStatus().equals("1")){
				p.setStatus_name("有效");
			}
			else{
				p.setStatus_name("过期");
			}
		}
		return pageList;
	}
}
